import ddApi from "@/components/publicJs/utils/ddApi";
import service from "@/api";
import dingtalkParams from '@/components/publicJs/dingtalkParams' // 钉钉参数配置





// 免登
async function authLogin() {
  const getUrlParamsRes = JSON.parse(localStorage.getItem('getUrlParamsRes'));
  // 获取临时授权码
  const requestAuthCodeRes = await ddApi.requestAuthCode(getUrlParamsRes.corpId);
  if (!requestAuthCodeRes.success) {
    return new Promise((resolve) => {
      resolve(requestAuthCodeRes);
    })
  }

  // 免登掉接口
  const getUserInfoRes = await service.getUserInfo({
    code: requestAuthCodeRes.data.code
  });
  if (!getUserInfoRes.success) {
    return new Promise((resolve) => {
      resolve(getUserInfoRes);
    })
  }

  // 设置请求头
  const setAuthorizationRes = await service.setAuthorization(getUserInfoRes.data.token);
  if (!setAuthorizationRes.success) {
    return new Promise((resolve) => {
      resolve(setAuthorizationRes);
    })
  }
  // 返回免登的结果
  return new Promise((resolve) => {
    resolve(getUserInfoRes);
  })
}


async function ddConfig(params) {
  const getPcJsConfigRes = await service.getPcJsConfig();
  if (!getPcJsConfigRes.success) {
    return new Promise((resolve) => {
      resolve(getPcJsConfigRes);
    })
  }
  let jsConfig = getPcJsConfigRes.data;
  jsConfig.jsApiList = dingtalkParams.jsApiList;
  const ddConfigRes = await ddApi.ddConfig(jsConfig);
  // 返回签名的结果
  return new Promise((resolve) => {
    resolve(getPcJsConfigRes);
  })
}





export {
  authLogin,
  ddConfig
}
